package com.m3958.logcollector.repository;

import javax.transaction.Transactional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.security.access.annotation.Secured;

import com.m3958.logcollector.domain.Person;
import com.m3958.logcollector.domain.ThirdPartLogin;
import com.m3958.logcollector.vo.RoleNames;

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends JpaRepository<Person, Long>, PersonRepositoryCustom, JpaSpecificationExecutor<Person> {

    Person findByEmail(@Param("email") String email);

    Person findByMobile(@Param("mobile") String mobile);

    Person findByName(@Param("name") String name);
    
    @Secured(RoleNames.PERSON_MANAGER)
    @Override
    Page<Person> findAll(Pageable pageable);

    @Override
    @Transactional
    public <S extends Person> S save(S entity);
    
    @Override
    @RestResource(exported = false)
    public void delete(Person entity);
    
    @Override
    @RestResource(exported = false)
    @Secured(RoleNames.PERSON_DELETER)
    public void deleteAll();

    @Override
    @Secured(RoleNames.USER)
    public Person findOne(Long personId);

    @Query(value = "SELECT p from Person p WHERE :tpl MEMBER OF p.thirdConns")
    Person findByThirdPartLoginId(@Param("tpl") ThirdPartLogin tpl);

}
